package com.yinhe.music.yhmusic.singer.list;

import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.SingerList;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;

public class SingerListPresenter extends BasePresenter<IBaseModel, ISingerListContract.ISingerListView>implements ISingerListContract.ISingerListPresenter {
    @Override
    public void getSingerList() {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        addSubscription(mModel.getSingerList()
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(singerList -> {
                            getView().hideLoading();
                            getView().setSingerListUI(singerList);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    @Override
    public void getSearchList(int page, int pageSize, String keyword) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        addSubscription(mModel.getSearchList(page, pageSize, keyword, IConstants.SINGER)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(searchInfo -> {
                            getView().hideLoading();
                            SingerList singerList = new SingerList(searchInfo.getPageNum(),searchInfo.getSingerList());
                            getView().setSingerListUI(singerList);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    @Override
    public void getCollectSingerList(int page, int pageSize) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        addSubscription(mModel.getCollectSinger(page, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(singerList -> {
                            getView().hideLoading();
                            getView().setSingerListUI(singerList);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }
}
